<%@ page contentType="text/html; charset=utf-8" %>
<%@ include file="/WEB-INF/jsp/includeCss.jsp"%>
<%@ include file="/WEB-INF/jsp/includeJs.jsp"%>
<%@ include file="/WEB-INF/jsp/includeTaglib.jsp"%>
<script type="text/javascript" src="<c:url value='/js/ztree.province.city.js' />"></script>
<style type="text/css">
  .hidden_check {}
  /*.hidden_check {display: none;}*/
</style>

<table width="100%" border="0" cellpadding="0" cellspacing="5">
  <tr>
    <td valign="top">
      <table class="dbborder" width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="bottom" class="blueTableBg">&nbsp;配置管理&nbsp;-&nbsp;新增角色权限</td>
        </tr>
        <tr>
          <td>
            <table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <td>
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                      <td class="dashTopLeft"><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                      <td class="dashTop" width=100%><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                      <td class="dashTopRight"><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                    </tr>
                    <tr>
                      <td class="dashLeft"><img src="<c:url value='/images/spacer.gif' />" width="7" height="7"></td>
                      <td align="center">
                      	<form:form target="tempFrame" commandName="permissionRoleForm" id="newPermissionRole" method="post" action="newPermissionRole.html">
                        <table width="100%" border="0" cellpadding="0" cellspacing="3" class="globalText">
	                          <tr style="vertical-align: top">
                              <td>
                                <input id="roleName" type="text" class="txtbox required" size="20" name="name" placeholder="请输入角色名称" style="margin-bottom: 10px">
                                <div class="mockTree">
                                  <dl>
                                    <dt>
                                      <input class="entries" id="chk_myPlanGroup" value="${MODULE_MYPLAN_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MYPLAN_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="syncCheck(this, 'chk_myPlan')" name="permissionEntries" type="checkbox">
                                      <label for="chk_myPlanGroup">我的计划任务权限</label>
                                      <input class="entries hidden_check" id="chk_myPlan" value="${MODULE_MYPLAN_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MYPLAN_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                    </dt>
                                    <dd>
                                      <input class="entries" id="chk_myPlanCoStoreVisiting" value="${MODULE_MYPLAN_CO_STOREVISITING_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MYPLAN_CO_STOREVISITING_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="noneBothCheck(this, 'chk_subPlanRequireAudit')" name="permissionEntries" type="checkbox">
                                      <label for="chk_myPlanCoStoreVisiting">指定协同巡店</label>
                                    </dd>
                                    <dt>
                                      <input class="entries" id="chk_subGroup" value="${MODULE_SUB_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="syncCheck(this, 'chk_sub')" name="permissionEntries" type="checkbox">
                                      <label for="chk_subGroup">下属工作跟踪权限</label>
                                      <input class="entries hidden_check" id="chk_sub" type="checkbox" name="permissionEntries" value="${MODULE_SUB_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}" />
                                    </dt>
                                    <dd>
                                      <input class="entries" id="chk_subTaskAssign" value="${MODULE_SUB_TASK_ASSIGN_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_TASK_ASSIGN_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                      <label for="chk_subTaskAssign">下属任务指派</label>
                                    </dd>
                                    <dd>
                                      <input class="entries" id="chk_subPlanTaskView" value="${MODULE_SUB_PLANTASK_VIEW_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_PLANTASK_VIEW_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="postConditionUncheck(this, 'chk_subPlanTaskAudit')" name="permissionEntries" type="checkbox">
                                      <label for="chk_subPlanTaskView">下属计划任务查看</label>
                                    </dd>
                                    <dd>
                                      <input class="entries" id="chk_subPlanTaskAudit" value="${MODULE_SUB_PLANTASK_AUDIT_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_PLANTASK_AUDIT_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="preConditionCheck(this, 'chk_subPlanTaskView')" name="permissionEntries" type="checkbox">
                                      <label for="chk_subPlanTaskAudit">下属计划任务审核</label>
                                    </dd>
                                    <dd>
                                      <input class="entries" id="chk_subPlanRequireAudit" value="${MODULE_SUB_PLAN_NEEDTOBE_AUDITED_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_SUB_PLAN_NEEDTOBE_AUDITED_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="noneBothCheck(this, 'chk_myPlanCoStoreVisiting')" name="permissionEntries" type="checkbox">
                                      <label for="chk_subPlanRequireAudit">下属计划需上级审核</label>
                                    </dd>
                                    <dt>
                                      <input class="entries" id="chk_projectGroup" value="${MODULE_PROJMGMT_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_PROJMGMT_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="syncCheck(this, 'chk_project')" name="permissionEntries" type="checkbox">
                                      <label for="chk_projectGroup">项目管理权限</label>
                                      <input class="entries hidden_check" id="chk_project" value="${MODULE_PROJMGMT_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_PROJMGMT_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                    </dt>
                                    <dt>
                                      <input class="entries" id="chk_messageGroup" value="${MODULE_MSG_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MSG_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="syncCheck(this, 'chk_message')" name="permissionEntries" type="checkbox">
                                      <label for="chk_messageGroup">消息公告权限</label>
                                      <input class="entries hidden_check" id="chk_message" value="${MODULE_MSG_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_MSG_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                    </dt>
                                    <dt>
                                      <input class="entries" id="chk_adminGroup" value="${MODULE_ADMIN_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="syncCheck(this, 'chk_admin')" name="permissionEntries" type="checkbox">
                                      <label for="chk_adminGroup">配置项权限</label>
                                      <input class="entries hidden_check" id="chk_admin" value="${MODULE_ADMIN_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                    </dt>
                                    <dd>
                                      <input class="entries" id="chk_adminUserACL" value="${MODULE_ADMIN_USR_ACL_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_USR_ACL_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                      <label for="chk_adminuseracl">用户权限管理</label>
                                    </dd>
                                    <dd>
                                      <input class="entries" id="chk_adminWorkflow" value="${MODULE_ADMIN_WORKFLOW_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_WORKFLOW_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                      <label for="chk_adminWorkflow">工作流程配置</label>
                                    </dd>
                                    <dd>
                                      <input class="entries" id="chk_adminProject" value="${MODULE_ADMIN_PROJECT_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_PROJECT_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                      <label for="chk_adminProject">项目管理配置</label>
                                    </dd>
                                    <dd>
                                      <input class="entries" id="chk_adminStore" value="${MODULE_ADMIN_STORE_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_STORE_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                      <label for="chk_adminStore">门店管理配置</label>
                                    </dd>
                                    <dd>
                                      <input class="entries" id="chk_adminRoleTask" value="${MODULE_ADMIN_ROLETASK_AIM_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_ADMIN_ROLETASK_AIM_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                      <label for="chk_adminRoleTask">员工任务目标配置</label>
                                    </dd>
                                    <dt>
                                      <input class="entries" id="chk_reportGroup" value="${MODULE_RPT_GROUP_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_RPT_GROUP_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             onchange="syncCheck(this, 'chk_report')" name="permissionEntries" type="checkbox">
                                      <label for="chk_reportGroup">分析报表权限</label>
                                      <input class="entries hidden_check" id="chk_report" value="${MODULE_RPT_CODE}${P_MOUDLE_ID_SEPARATOR}${MODULE_RPT_TYPE}${P_MOUDLE_ID_SEPARATOR}${P_ADMIN_MASK}"
                                             name="permissionEntries" type="checkbox">
                                    </dt>
                                    <dd>
                                      <input type="checkbox" id="useADFilter" name="useADFilter" value="1"
                                             onchange="postConditionUncheck(this, 'isAdvanced')">
                                      <label for="useADFilter">报表区域限制 ==&gt;</label>
                                    </dd>
                                    <dt>
                                      <input type="checkbox" id="isAdvanced" name="isAdvanced" value="1"
                                             onchange="preConditionCheck(this, 'chk_reportGroup'); preConditionCheck(this, 'useADFilter')">
                                      <label for="isAdvanced">高级管理者 ==&gt;</label>
                                    </dt>
                                  </dl>
                                  <input type="button" class="btnStyle" value="保存" onclick="doSubmit()" style="margin-top: 10px">
                                </div>
                              </td>
                              <td>
                              <span class="mockTree" id="useADFilterPane" style="width: 200px;">
                                <label>报表区域限制</label>
                                <span id="adTree" class="ztree"></span>
                              </span>
                              </td>
                              <td>
                              <span class="mockTree" id="isAdvancedPane">
                                <label>高级管理者项目查阅权限</label>
                                <span id="orgTree" class="ztree"></span>
                              </span>
                              </td>
	                          </tr>
                        </table>
                        <input type="hidden" name="managedAdminDivisionCodeEntries" id="divisionCodes" />
                        <input type="hidden" name="managedOrgCodeEntries" id="orgNodes" />
                        <input type="hidden" name="updatekey" value="-1"/>
                        </form:form>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>


<script type="text/javascript">
  function noneBothCheck(src, dst_id) {
    $(src).prop('checked') && $('#' + dst_id).prop('checked', false);
    updateUI();
  }

  function preConditionCheck(src, dst_id) {
    $(src).prop('checked') && $('#' + dst_id).prop('checked', true);
    updateUI();
  }

  function postConditionUncheck(src, dst_id) {
    $(src).prop('checked') || $('#' + dst_id).prop('checked', false);
    updateUI();
  }

  function syncCheck(src, dst_id) {
    $('#' + dst_id).prop('checked', $(src).prop('checked'));
    updateUI();
  }

  function updateUI() {
    //我的计划任务
    if ($('#chk_myPlanGroup').prop('checked')) {
      $('#chk_myPlanCoStoreVisiting').removeAttr('disabled');
    } else {
      $('#chk_myPlanCoStoreVisiting').prop('checked', false).attr('disabled', 'disabled');
    }
    //下属工作跟踪
    if ($('#chk_subGroup').prop('checked')) {
      $('#chk_subTaskAssign').removeAttr('disabled');
      $('#chk_subPlanTaskView').removeAttr('disabled');
      $('#chk_subPlanTaskAudit').removeAttr('disabled');
      $('#chk_subPlanRequireAudit').removeAttr('disabled');
    } else {
      $('#chk_subTaskAssign').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_subPlanTaskView').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_subPlanTaskAudit').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_subPlanRequireAudit').prop('checked', false).attr('disabled', 'disabled');
    }
    //配置管理
    if ($('#chk_adminGroup').prop('checked')) {
      $('#chk_adminUserACL').removeAttr('disabled');
      $('#chk_adminWorkflow').removeAttr('disabled');
      $('#chk_adminProject').removeAttr('disabled');
      $('#chk_adminStore').removeAttr('disabled');
      $('#chk_adminRoleTask').removeAttr('disabled');
    } else {
      $('#chk_adminUserACL').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_adminWorkflow').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_adminProject').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_adminStore').prop('checked', false).attr('disabled', 'disabled');
      $('#chk_adminRoleTask').prop('checked', false).attr('disabled', 'disabled');
    }
    //报表
    if ($('#chk_reportGroup').prop('checked')) {
      $('#useADFilter').removeAttr('disabled');
    } else {
      $('#useADFilter').prop('checked', false).attr('disabled', 'disabled');
      $('#isAdvanced').prop('checked', false);
    }
    //高级管理者
    if ($('#isAdvanced').prop('checked')) {
      $('#chk_reportGroup').removeAttr('disabled').prop('checked', true);
      $('#chk_report').prop('checked', true);
      $('#useADFilter').removeAttr('disabled').prop('checked', true);
    }
  }

	function submitCallbackSuccess(obj){
		window.location="<c:url value='/roleList.html' />";
	}

  function doSubmit(){
    $('#roleName').val($('#roleName').val().trim());
    if ($('#roleName').val().length == 0) {
      alert('请输入角色名称');
      $('#roleName').focus();
      return;
    }
    var divisionCodes = '';
    if ($('#useADFilter').prop('checked')) {
      var divisionNode = $.fn.zTree.getZTreeObj("adTree").getCheckedNodes(true);
      divisionNode.forEach(function(e) {
        e.getCheckStatus().half || (divisionCodes += e.code + ',')
      });
      divisionCodes.length > 0 && (divisionCodes = divisionCodes.slice(0, -1))
    }
    var orgNodes = '';
    if ($('#isAdvanced').prop('checked')) {
      var orgNode = $.fn.zTree.getZTreeObj("orgTree").getCheckedNodes(true);
      orgNode.forEach(function(e) {
        e.getCheckStatus().half || (orgNodes += e.id + ',')
      });
      orgNodes.length > 0 && (orgNodes = orgNodes.slice(0, -1))
    }
    $('#divisionCodes').val(divisionCodes);
    $('#orgNodes').val(orgNodes);
    $('#newPermissionRole').submit();
  }

  (function() {
    $.getJSON("<c:url value='/json/findAllOrgnizations.html' />", function(zNodes) {
      $.fn.zTree.init($("#orgTree"), {check: {enable: true}, data: {simpleData: {enable: true}}}, zNodes).expandAll(true);
    });

    var tree = $.fn.zTree.init($('#adTree'), {check: {enable: true}}, ztree_province_city);
    tree.expandNode(tree.getNodes()[0], true);

    updateUI();
  })();

</script>
